package com.byedu.focus;

import com.byedu.base.BaseService;
import com.byedu.domain.focus.FocusBugetDO;
import com.byedu.domain.focus.FocusProduceDO;
import com.byedu.domain.focus.FocusProjectDO;
import com.byedu.domain.focus.FocusUserDO;
import com.byedu.domain.focus.vo.enumvos.*;
import com.byedu.domain.focus.vo.FocusProjectVO;
import com.byedu.domain.focus.vo.FocusUserVO;
import com.byedu.mapper.focus.*;
import com.byedu.util.CommonUtil;
import com.byedu.util.yunpian.util.StringUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jason on 2017/5/14.
 */
@Service
public class FocusProjectService extends BaseService {

    @Autowired
    FocusProjectMapper focusProjectMapper;
    @Autowired
    FocusProduceMapper focusProduceMapper;
    @Autowired
    FocusUserMapper focusUserMapper;
    @Autowired
    FocusKpiMapper focusKpiMapper;
    @Autowired
    FocusBugetMapper focusBugetMapper;

    @Autowired
    FocusPreBugetMapper focusPreBugetMapper;

    public List<FocusProjectVO> getFocusProjectVOList() {
        List<FocusProjectDO>  focusProjectDOList = focusProjectMapper.getFocusProjectDOList();
        List<FocusProduceDO>  focusProduceDOList = focusProduceMapper.getFocusProduceDOList();

        List<FocusUserDO>  focusUserDOList = focusUserMapper.getFoucUserList();
        List<FocusProjectVO> focusProjectVOList = new ArrayList<FocusProjectVO>();

        for(FocusProjectDO focusProjectDO:focusProjectDOList){
            FocusProjectVO focusProjectVO = new FocusProjectVO();
            try {
                BeanUtils.copyProperties(focusProjectVO, focusProjectDO);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            for(FocusProduceDO focusProduceDO:focusProduceDOList){
                if(focusProjectDO.getProduceid().equals(focusProduceDO.getId())){
                    focusProjectVO.setProduce(focusProduceDO);
                }
            }

            for(FocusUserDO focusUserDO: focusUserDOList){
                FocusUserVO focusUserVO = new FocusUserVO(focusUserDO);
                if(focusProjectDO.getCoid().equals(focusUserVO.getId())){
                    focusProjectVO.setCo(focusUserVO);
                }
                if(focusProjectDO.getAeid().equals(focusUserVO.getId())){
                    focusProjectVO.setAe(focusUserVO);
                }
            }
//            focusProjectVO.setFocusce(new FocusCategoryTypeVO(CommonUtil.StringToInt(focusProjectDO.getFocuscategory())));
            focusProjectVO.setStatusTypeVO(new FocusStatusTypeVO(CommonUtil.StringToInt(focusProjectDO.getStatus())));
            focusProjectVOList.add(focusProjectVO);
        }
        return focusProjectVOList;
    }

    public List<FocusProjectDO> getFocusProjectDOList() {
        return focusProjectMapper.getFocusProjectDOList();
    }



    @Transactional
    public void updateFocusPreBuget(int projectId, List<FocusBugetDO> focusBugets){

        focusPreBugetMapper.delFocusBugetbyprojectId(projectId);
        System.out.println(focusBugets.toString());
        double money = 0;
        for(int i =0;i<focusBugets.size();i++){
            money = money + Double.parseDouble(focusBugets.get(i).getMoney());
        }
        FocusProjectDO focusProjectDO = new FocusProjectDO();
        focusProjectDO.setId(String.valueOf(projectId));
        focusProjectDO.setPlancomoutnum(String.valueOf(money));
        focusProjectDO.setRealcomoutnum(String.valueOf(money));
        focusProjectMapper.updateFocusProjectDObyId(focusProjectDO);
        focusPreBugetMapper.addFocusBugetList(focusBugets);
        //复制到实际支出项中
        focusBugetMapper.addFocusBugetList(focusBugets);

    }

    @Transactional
    public void updateFocusBuget(int projectId, List<FocusBugetDO> focusBugets){

        focusBugetMapper.delFocusBugetbyprojectId(projectId);
        System.out.println(focusBugets.toString());
        double money = 0;
        for(int i =0;i<focusBugets.size();i++){
            money = money + Double.parseDouble(focusBugets.get(i).getMoney());
        }
        FocusProjectDO focusProjectDO = new FocusProjectDO();
        focusProjectDO.setId(String.valueOf(projectId));
        focusProjectDO.setRealcomoutnum(String.valueOf(money));
        focusProjectMapper.updateFocusProjectDObyId(focusProjectDO);
        focusBugetMapper.addFocusBugetList(focusBugets);

    }


    public FocusProjectVO getFocusProjectbyId(String id) {

        List<FocusProduceDO>  focusProduceDOList = focusProduceMapper.getFocusProduceDOList();

        List<FocusUserDO>  focusUserDOList = focusUserMapper.getFoucUserList();

        FocusProjectDO focusProjectDO =  focusProjectMapper.getFocusProjectbyId(id);
        FocusProjectVO focusProjectVO = new FocusProjectVO();
        try {
            if(focusProjectDO !=null) {
                BeanUtils.copyProperties(focusProjectVO, focusProjectDO);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        for(FocusProduceDO focusProduceDO:focusProduceDOList){
            if(focusProjectDO.getProduceid().equals(focusProduceDO.getId())){
                focusProjectVO.setProduce(focusProduceDO);
            }
        }

        for(FocusUserDO focusUserDO: focusUserDOList){
            FocusUserVO focusUserVO = new FocusUserVO(focusUserDO);
            if(focusProjectDO.getCoid().equals(focusUserVO.getId())){
                focusProjectVO.setCo(focusUserVO);
            }
            if(focusProjectDO.getAeid().equals(focusUserVO.getId())){
                focusProjectVO.setAe(focusUserVO);
            }
        }
//        focusProjectVO.setFocusce(new FocusCategoryTypeVO(CommonUtil.StringToInt(focusProjectDO.getFocuscategory())));
        focusProjectVO.setStatusTypeVO(new FocusStatusTypeVO(CommonUtil.StringToInt(focusProjectDO.getStatus())));

        return focusProjectVO;
    }

    public List<FocusProjectDO> getWorkerFocusProjectByFocusProjectDO(FocusProjectDO focusProjectDO){

        return focusProjectMapper.getWorkerFocusProjectByFocusProjectDO(focusProjectDO);
    }
    public List<FocusProjectDO> getWorkerFocusProjectByAllFocusProjectDO(){

        return focusProjectMapper.getFocusProjectDOList();
    }

    public List<FocusCategoryTypeVO> getFocusCategoryTypeVOList() {
        List<FocusCategoryTypeVO> list = new ArrayList<FocusCategoryTypeVO>();

        for (FocusCategoryTypeEnum focusCategoryTypeEnum : FocusCategoryTypeEnum.values()) {
            FocusCategoryTypeVO focusCategoryTypeVO = new FocusCategoryTypeVO(focusCategoryTypeEnum);

            list.add(focusCategoryTypeVO);
        }
        return list;
    }

    public List<FocusStatusTypeVO> getFocusStatusTypeVOList() {
        List<FocusStatusTypeVO> list = new ArrayList<FocusStatusTypeVO>();

        for (FocusStatusTypeEnum focusStatusTypeEnum : FocusStatusTypeEnum.values()) {
            FocusStatusTypeVO focusStatusTypeVO = new FocusStatusTypeVO(focusStatusTypeEnum);

            list.add(focusStatusTypeVO);
        }
        return list;
    }

    public Integer addFocusProjectDO(FocusProjectDO focusProjectDO){
        return focusProjectMapper.addFocusProjectDO(focusProjectDO);
    }

    public Integer delFocusProjectDO(FocusProjectDO focusProjectDO){
        return focusProjectMapper.delFocusProjectDO(focusProjectDO);
    }
    public Integer updateFocusProjectDObyId(FocusProjectDO focusProjectDO){
        return focusProjectMapper.updateFocusProjectDObyId(focusProjectDO);
    }




    public List<FocusProjectVO> getFocusProjectVOListByTimeandprid(String id ,String[] timeStr, String ptid,String name,String aeid,String statuid) throws UnsupportedEncodingException {
        FocusProjectDO  searchFocusProjectDO = new FocusProjectDO();
        if(timeStr != null){
            searchFocusProjectDO.setPryear(timeStr[0]);
            searchFocusProjectDO.setPrjidu(timeStr[1]);
        }
        if(!StringUtil.isNullOrEmpty(id)) {
            searchFocusProjectDO.setAeid(id);
        }
        if(!StringUtil.isNullOrEmpty(aeid)) {
            searchFocusProjectDO.setAeid(aeid);
        }
        if(!StringUtil.isNullOrEmpty(statuid)) {
            searchFocusProjectDO.setStatus(statuid);
        }
        if(!StringUtil.isNullOrEmpty(ptid)) {
            searchFocusProjectDO.setProduceid(ptid);
        }
        if(!StringUtil.isNullOrEmpty(name)){
            name = new String(name.getBytes("iso8859-1"), "utf-8");
            searchFocusProjectDO.setName(name);
        }

        List<FocusProjectDO>  focusProjectDOList = focusProjectMapper.getFocusProjectDOListByFocusProjectDO(searchFocusProjectDO);
        List<FocusProduceDO>  focusProduceDOList = focusProduceMapper.getFocusProduceDOList();

        List<FocusUserDO>  focusUserDOList = focusUserMapper.getFoucUserList();
        List<FocusProjectVO> focusProjectVOList = new ArrayList<FocusProjectVO>();

        for(FocusProjectDO focusProjectDO:focusProjectDOList){
            FocusProjectVO focusProjectVO = new FocusProjectVO();
            try {
                BeanUtils.copyProperties(focusProjectVO, focusProjectDO);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            for(FocusProduceDO focusProduceDO:focusProduceDOList){
                if(focusProjectDO.getProduceid().equals(focusProduceDO.getId())){
                    focusProjectVO.setProduce(focusProduceDO);
                }
            }

            for(FocusUserDO focusUserDO: focusUserDOList){
                FocusUserVO focusUserVO = new FocusUserVO(focusUserDO);
                if(focusProjectDO.getCoid().equals(focusUserVO.getId())){
                    focusProjectVO.setCo(focusUserVO);
                }
                if(focusProjectDO.getAeid().equals(focusUserVO.getId())){
                    focusProjectVO.setAe(focusUserVO);
                }
            }
//            focusProjectVO.setFocusce(new FocusCategoryTypeVO(CommonUtil.StringToInt(focusProjectDO.getFocuscategory())));
            focusProjectVO.setStatusTypeVO(new FocusStatusTypeVO(CommonUtil.StringToInt(focusProjectDO.getStatus())));
            focusProjectVOList.add(focusProjectVO);
        }
        return focusProjectVOList;
    }


    public List<FocusProjectVO> getFocusUnfinishedProjectVOListByTimeandprid(String id ,String[] timeStr, String ptid,String name,String aeid,String statuid ,int fl) {
        FocusProjectDO  searchFocusProjectDO = new FocusProjectDO();
        if(timeStr != null){
            searchFocusProjectDO.setPryear(timeStr[0]);
            searchFocusProjectDO.setPrjidu(timeStr[1]);
        }
        if(!StringUtil.isNullOrEmpty(aeid)) {
            searchFocusProjectDO.setAeid(aeid);
        }
        if(!StringUtil.isNullOrEmpty(id)) {
            searchFocusProjectDO.setAeid(id);
        }

        if(!StringUtil.isNullOrEmpty(statuid)) {
            searchFocusProjectDO.setStatus(statuid);
        }
        if(!StringUtil.isNullOrEmpty(ptid)) {
            searchFocusProjectDO.setProduceid(ptid);
        }
        if(!StringUtil.isNullOrEmpty(name)){
            try {
                name = new String(name.getBytes("iso8859-1"), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            searchFocusProjectDO.setName(name);
        }

        List<FocusProjectDO>  focusProjectDOList = null;
        switch (fl){
            case 1 :
                focusProjectDOList= focusProjectMapper.getFocusProjectUnfinishedBOSSDOListByFocusProjectDO(searchFocusProjectDO);
                break;
            case 2 :
                focusProjectDOList= focusProjectMapper.getFocusProjectUnfinishedAEDOListByFocusProjectDO(searchFocusProjectDO);

                break;
            case 3 :
                searchFocusProjectDO.setAeid(null);
                focusProjectDOList= focusProjectMapper.getFocusProjectUnfinishedACCOUNTDOListByFocusProjectDO(searchFocusProjectDO);
                break;
            case 4 :
                searchFocusProjectDO.setAeid(null);
                focusProjectDOList= focusProjectMapper.getFocusProjectUnfinishedCASHIERDOListByFocusProjectDO(searchFocusProjectDO);
                break;
            case 5 :
                break;
        }


        List<FocusProduceDO>  focusProduceDOList = focusProduceMapper.getFocusProduceDOList();

        List<FocusUserDO>  focusUserDOList = focusUserMapper.getFoucUserList();
        List<FocusProjectVO> focusProjectVOList = new ArrayList<FocusProjectVO>();
        if(focusProjectDOList != null) {
            for (FocusProjectDO focusProjectDO : focusProjectDOList) {
                FocusProjectVO focusProjectVO = new FocusProjectVO();
                try {
                    BeanUtils.copyProperties(focusProjectVO, focusProjectDO);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }

                for (FocusProduceDO focusProduceDO : focusProduceDOList) {
                    if (focusProjectDO.getProduceid().equals(focusProduceDO.getId())) {
                        focusProjectVO.setProduce(focusProduceDO);
                    }
                }

                for (FocusUserDO focusUserDO : focusUserDOList) {
                    FocusUserVO focusUserVO = new FocusUserVO(focusUserDO);
                    if (focusProjectDO.getCoid().equals(focusUserVO.getId())) {
                        focusProjectVO.setCo(focusUserVO);
                    }
                    if (focusProjectDO.getAeid().equals(focusUserVO.getId())) {
                        focusProjectVO.setAe(focusUserVO);
                    }
                }
//            focusProjectVO.setFocusce(new FocusCategoryTypeVO(CommonUtil.StringToInt(focusProjectDO.getFocuscategory())));
                focusProjectVO.setStatusTypeVO(new FocusStatusTypeVO(CommonUtil.StringToInt(focusProjectDO.getStatus())));
                focusProjectVOList.add(focusProjectVO);
            }
        }
        return focusProjectVOList;
    }
    public List<FocusProjectVO> getFocushasFinishedProjectVOListByTimeandprid(String id ,String[] timeStr, String ptid,String name,String aeid, String statuid) throws UnsupportedEncodingException {
        FocusProjectDO  searchFocusProjectDO = new FocusProjectDO();
        if(timeStr != null){
            searchFocusProjectDO.setPryear(timeStr[0]);
            searchFocusProjectDO.setPrjidu(timeStr[1]);
        }
        if(!StringUtil.isNullOrEmpty(id)) {
            searchFocusProjectDO.setAeid(id);
        }
        if(!StringUtil.isNullOrEmpty(aeid)) {
            searchFocusProjectDO.setAeid(aeid);
        }
        if(!StringUtil.isNullOrEmpty(statuid)) {
            searchFocusProjectDO.setStatus(statuid);
        }
        if(!StringUtil.isNullOrEmpty(ptid)) {
            searchFocusProjectDO.setProduceid(ptid);
        }
        if(!StringUtil.isNullOrEmpty(name)){
            name = new String(name.getBytes("iso8859-1"), "utf-8");
            searchFocusProjectDO.setName(name);
        }

        List<FocusProjectDO>  focusProjectDOList = focusProjectMapper.getFocusProjectHasfinishedDOListByFocusProjectDO(searchFocusProjectDO);
        List<FocusProduceDO>  focusProduceDOList = focusProduceMapper.getFocusProduceDOList();

        List<FocusUserDO>  focusUserDOList = focusUserMapper.getFoucUserList();
        List<FocusProjectVO> focusProjectVOList = new ArrayList<FocusProjectVO>();

        for(FocusProjectDO focusProjectDO:focusProjectDOList){
            FocusProjectVO focusProjectVO = new FocusProjectVO();
            try {
                BeanUtils.copyProperties(focusProjectVO, focusProjectDO);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            for(FocusProduceDO focusProduceDO:focusProduceDOList){
                if(focusProjectDO.getProduceid().equals(focusProduceDO.getId())){
                    focusProjectVO.setProduce(focusProduceDO);
                }
            }

            for(FocusUserDO focusUserDO: focusUserDOList){
                FocusUserVO focusUserVO = new FocusUserVO(focusUserDO);
                if(focusProjectDO.getCoid().equals(focusUserVO.getId())){
                    focusProjectVO.setCo(focusUserVO);
                }
                if(focusProjectDO.getAeid().equals(focusUserVO.getId())){
                    focusProjectVO.setAe(focusUserVO);
                }
            }
//            focusProjectVO.setFocusce(new FocusCategoryTypeVO(CommonUtil.StringToInt(focusProjectDO.getFocuscategory())));
            focusProjectVO.setStatusTypeVO(new FocusStatusTypeVO(CommonUtil.StringToInt(focusProjectDO.getStatus())));
            focusProjectVOList.add(focusProjectVO);
        }
        return focusProjectVOList;
    }



    public List<FocusProjectVO> getFocushasProcessProjectVOListByTimeandprid(String id ,String[] timeStr, String ptid,String name,String aeid, String statuid) throws UnsupportedEncodingException {
        FocusProjectDO  searchFocusProjectDO = new FocusProjectDO();
        if(timeStr != null){
            searchFocusProjectDO.setPryear(timeStr[0]);
            searchFocusProjectDO.setPrjidu(timeStr[1]);
        }
        if(!StringUtil.isNullOrEmpty(id)) {
            searchFocusProjectDO.setAeid(id);
        }
        if(!StringUtil.isNullOrEmpty(aeid)) {
            searchFocusProjectDO.setAeid(aeid);
        }
        if(!StringUtil.isNullOrEmpty(statuid)) {
            searchFocusProjectDO.setStatus(statuid);
        }
        if(!StringUtil.isNullOrEmpty(ptid)) {
            searchFocusProjectDO.setProduceid(ptid);
        }
        if(!StringUtil.isNullOrEmpty(name)){
            name = new String(name.getBytes("iso8859-1"), "utf-8");
            searchFocusProjectDO.setName(name);
        }

        List<FocusProjectDO>  focusProjectDOList = focusProjectMapper.getFocusProjectProcessBOSSDOListByFocusProjectDO(searchFocusProjectDO);
        List<FocusProduceDO>  focusProduceDOList = focusProduceMapper.getFocusProduceDOList();

        List<FocusUserDO>  focusUserDOList = focusUserMapper.getFoucUserList();
        List<FocusProjectVO> focusProjectVOList = new ArrayList<FocusProjectVO>();

        for(FocusProjectDO focusProjectDO:focusProjectDOList){
            FocusProjectVO focusProjectVO = new FocusProjectVO();
            try {
                BeanUtils.copyProperties(focusProjectVO, focusProjectDO);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }

            for(FocusProduceDO focusProduceDO:focusProduceDOList){
                if(focusProjectDO.getProduceid().equals(focusProduceDO.getId())){
                    focusProjectVO.setProduce(focusProduceDO);
                }
            }

            for(FocusUserDO focusUserDO: focusUserDOList){
                FocusUserVO focusUserVO = new FocusUserVO(focusUserDO);
                if(focusProjectDO.getCoid().equals(focusUserVO.getId())){
                    focusProjectVO.setCo(focusUserVO);
                }
                if(focusProjectDO.getAeid().equals(focusUserVO.getId())){
                    focusProjectVO.setAe(focusUserVO);
                }
            }
//            focusProjectVO.setFocusce(new FocusCategoryTypeVO(CommonUtil.StringToInt(focusProjectDO.getFocuscategory())));
            focusProjectVO.setStatusTypeVO(new FocusStatusTypeVO(CommonUtil.StringToInt(focusProjectDO.getStatus())));
            focusProjectVOList.add(focusProjectVO);
        }
        return focusProjectVOList;
    }


    public List<FocusBugetDO> getFocusBugetDOList(String projectid) {

        return  focusBugetMapper.geFocusBugetDObyprojestId(projectid);
    }

    public List<FocusBugetDO> getFocusPreBugetDOList(String projectid) {

        return  focusPreBugetMapper.geFocusBugetDObyprojestId(projectid);
    }
}